<?php
// @author: Peter.Nguyen
// @date: 06/05/2012
// @function: _autoload class in cms
function __autoload($className)
{
	if (function_exists('smartyAutoload') AND smartyAutoload($className))
		return true;
	$className = str_replace(chr(0), '', $className);
	$arrPath = explode("_", $className);
	$path = "";
	$class= "";
	$lengPath = count($arrPath);
	foreach($arrPath as $key=>$val){
		if($key==$lengPath-1){
			$class = $val;
		}else{
			$path .= $val."/";	
		}
	}
	$classDir = dirname(__FILE__).'/../libraries/';
	$pluginsDir = dirname(__FILE__).'/../plugins/';
	$modulesDir = dirname(__FILE__).'/../modules/';
	$initClass = false;
	// Load class
	if ($initClass == false) {
		$pathClass = strtolower($classDir.$path.$class.'.php');
		$file_in_classes = file_exists($pathClass);
		if($file_in_classes){
			require_once($pathClass);
			$initClass = true;
		}
	}
	// Load plugin
	if ($initClass == false) {

		$classPlugins = str_replace('Plugin', '', $className);	
		$pathClass = strtolower($pluginsDir.$classPlugins."/".$classPlugins.'.plugin.php');		
		$file_in_classes = file_exists($pathClass);
		if($file_in_classes){
			require_once($pathClass);
			$initClass = true;
		}
	}
	// Load module
	$initClass = false;
	if ($initClass == false && $handle = opendir($modulesDir)) {	
		while (false !== ($entry = readdir($handle))) {
			$classModule = str_replace('Module', '', $className);
			$pathClass = strtolower($modulesDir."/".$entry."/".$classModule.'.module.php');
			$file_in_classes = file_exists($pathClass);
			if($file_in_classes){
				require_once($pathClass);
				$initClass = true;
			}
		}
	}		

}
function __autoload1($className)
{
	if (function_exists('smartyAutoload') AND smartyAutoload($className))
		return true;
	echo $className;die;
	$className = str_replace(chr(0), '', $className);
	$classDir = dirname(__FILE__).'/../classes/';
	$pluginsDir = dirname(__FILE__).'/../plugins/';
	$modulesDir = dirname(__FILE__).'/../modules/';
	$initClass = false;
	// Load class
	if ($initClass == false && $handle = opendir($classDir)) {
		while (false !== ($entry = readdir($handle))) {
			$pathClass = strtolower($classDir.$entry."/".$className.'.php');
			$file_in_classes = file_exists($pathClass);
			if($file_in_classes){
				require_once($entry."/".$className.'.php');
				$initClass = true;
				break;
			}
		}
		closedir($handle);
	}
	// Load plugin
	if ($initClass == false) {

		$classPlugins = str_replace('Plugin', '', $className);	
		$pathClass = strtolower($pluginsDir.$classPlugins."/".$classPlugins.'.php');
		if($className == "Login"){
			die($pathClass);
		}			
		$file_in_classes = file_exists($pathClass);
		if($file_in_classes){
			require_once($classPlugins."/".$classPlugins.'.php');
			$initClass = true;
		}
	}
	// Load module
	$initClass = false;
	if ($initClass == false && $handle = opendir($modulesDir)) {	
		while (false !== ($entry = readdir($handle))) {
			$classModule = str_replace('Module', '', $className);
			$pathClass = strtolower($modulesDir."/".$entry."/".$classModule.'.php');
			$file_in_classes = file_exists($pathClass);
			if($file_in_classes){
				require_once($entry."/".$classModule.'.php');
				$initClass = true;
			}
		}
	}
}